package com.zx.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zx.DAO.RoleDao;
import com.zx.DAO.UserDao;
import com.zx.entity.Role;
import com.zx.entity.User;
import com.zx.service.RoleInte;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 程仟磊
 * @date 2021/7/24
 */
@Service
public class RoleImpl implements RoleInte {
    private static final int PAGESIZE=3;
    private int pages;
    @Resource
    private RoleDao roleDao;
    @Resource
    private UserDao userDao;
    @Override
    public List<Role> findAllRole(String page) {
        Page<Object> objects = PageHelper.startPage(Integer.parseInt(page), PAGESIZE);
        List<Role> allRole = roleDao.findAllRole();
        pages= objects.getPages();
        return allRole;
    }

    @Override
    public List<Role> findRoleNoUid(String usid) {
        return roleDao.findRoleNoUid(Integer.parseInt(usid));
    }

    @Override
    public int getPages() {
        return pages;
    }

    @Override
    public void addMenuToRole(String rid, String[] mid) {
        for (int i = 0; i < mid.length; i++) {
            roleDao.addMenuToRole(Integer.parseInt(rid),Integer.parseInt(mid[i]));
        }
    }

    @Override
    public int findAccount(String rname) {
        return roleDao.findRoleByRname(rname);
    }

    @Override
    public void addRole(String rname) {
        roleDao.addRole(rname);
    }

    @Override
    public void deleteRole(String rid) {
        int i = roleDao.deleteUr(Integer.parseInt(rid));
        int i1 = roleDao.deleteRm(Integer.parseInt(rid));
        if (i>=0&&i1>=0){
            roleDao.deleteRole(Integer.parseInt(rid));
        }
    }
}
